import configparser
import logging

Parser: configparser.RawConfigParser()


def loadenv(env="fat", path="./conf/config-${env}.ini"):
    global Parser
    Parser = load(env, path)


def load(env="fat", path="./conf/config-${env}.ini"):
    filename = path.replace("${env}", env)
    try:
        parser = configparser.RawConfigParser()
        parser.read(filename, encoding="UTF-8")
        return parser
    except Exception as err:
        logging.exception(f"config >> {filename}:\n{err}")
        exit(0)


def get(prefix, key):
    try:
        return Parser.get(prefix, key)
    except Exception as err:
        raise Exception(f"当前Key值不存在 >> [{prefix}].{key}:\n{err}")
